package com.lyy.weather.database;

import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.lyy.weather.bean.City;
import com.lyy.weather.network.HttpUtil;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

//用于更新的工具类
public class RefreshUtil {
    private DatabaseUtil dbUtil;
    private List<String> locationIdList;
    private String locationId;//当前城市/地区的编号

    public RefreshUtil(DatabaseUtil dbUtil) {
        this.dbUtil = dbUtil;
        //查询数据库中所有的城市编号
        this.locationIdList=dbUtil.getAlllocationId();
       // Log.d("TAG", locationIdList.toString());
    }

    //更新所有数据的方法
    public void refreshAll(){
        //删除数据库中除城市信息外的所有表的数据
        dbUtil.deleteAll();
        //下面遍历城市编号，更新数据
        Iterator<String> iterator=locationIdList.iterator();

        while(iterator.hasNext()){
            locationId=iterator.next();
            HttpUtil httpUtil=new HttpUtil(locationId,HttpUtil.MyRequestType.ALL_DATA_REQUEST,dbUtil.getmContext(),dbUtil.getDbHelper());
            httpUtil.getAllDataOfSpecificLocation();
        }

        dbUtil.addTime();
        Log.d("TAG", "RefreshAll: finished~~");
    }

    //更新近24小时天气的方法
    public void refresh24hWeather(){
        dbUtil.deleteAll24hData();
        Iterator<String> iterator=locationIdList.iterator();

        while(iterator.hasNext()){
            String locationId=iterator.next();
            HttpUtil httpUtil=new HttpUtil(locationId,HttpUtil.MyRequestType.WEATHER_HOURLY_REQUEST,dbUtil.getmContext(),dbUtil.getDbHelper());
            httpUtil.getDataFromApi();
        }
        dbUtil.addTime();
        Log.d("TAG", "Refresh24hWeather: finished~~");
    }

    public DatabaseUtil getDbUtil() {
        return dbUtil;
    }

    public List<String> getLocationIdList() {
        return locationIdList;
    }
}
